// releasePage/pages/addOperator/chooseChannel/chooseChannel.js
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        type: Number, //type  00--LTE频段  01--NR频段
        idArr: '', //再次编辑已选择的id
        ipxBottom: 18,
        index: 0,
        deviceContent: [],
        showCover: false,
        showEdit: false, //编辑显示
        inputValue: '',
        id: '', //频段id
        sceneDescription: '',
        btnActive: false, //确定按钮
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({ type: options.type, idArr: options.id }, () => {
            this.getNetDeviceList();
        });
        if (app.globalData.isIpx) {
            this.setData({
                ipxBottom: 52,
            });
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    refresh() {
        let that = this;
        that.setData({});
        that.getNetDeviceList();
    },
    //编辑模式
    showEdit(e) {
        if (app.powerIdentity('00,01')) return; //权限判断
        this.setData({
            showEdit: !this.data.showEdit,
        });
    },
    //关闭编辑模式
    closeEdit(e) {
        if (app.powerIdentity('00,01')) return; //权限判断
        this.setData({
            showEdit: !this.data.showEdit,
        });
    },
    //显示添加网络场景
    showCover(e) {
        if (app.powerIdentity('00,01')) return; //权限判断
        this.setData({
            showCover: true,
            inputValue: '',
            id: '',
            sceneDescription: '',
        });
    },
    getScene(e) {
        let str = e.detail.value;
        if (str) {
            if (this.data.type == '00') {
                if (str.substring(0, 1) == 'B') {
                    this.setData({
                        inputValue: str,
                    });
                } else {
                    if (str.substring(0, 1) == 'b') {
                        this.setData({
                            inputValue: 'B' + str.substr(1),
                        });
                    } else {
                        wx.showToast({
                            title: 'LTE的频段号必须是“B”开头',
                            icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                            duration: 1500,
                        });
                        this.setData({
                            inputValue: '',
                        });
                    }
                }
            }
            if (this.data.type == '01') {
                if (str.substring(0, 1) == 'n') {
                    this.setData({
                        inputValue: str,
                    });
                } else {
                    if (str.substring(0, 1) == 'N') {
                        this.setData({
                            inputValue: 'n' + str.substr(1),
                        });
                    } else {
                        wx.showToast({
                            title: 'NR的频段号必须是“n”开头',
                            icon: 'none', //如果要纯文本，不要icon，将值设为'none'
                            duration: 1500,
                        });
                        this.setData({
                            inputValue: '',
                        });
                    }
                }
            }
        }
    },
    sceneDescription(e) {
        this.setData({
            sceneDescription: e.detail.value,
        });
    },
    //选择网络场景
    chooseScene(e) {
        let arr = this.data.deviceContent;
        arr[e.currentTarget.dataset.index].select = !arr[
            e.currentTarget.dataset.index
        ].select;
        this.setData({
            deviceContent: arr,
        });
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].select) {
                this.setData({
                    btnActive: true,
                });
                return;
            } else {
                this.setData({
                    btnActive: false,
                });
            }
        }
    },
    //编辑网络设备商
    editScene(e) {
        this.showCover();
        this.setData({
            inputValue: e.currentTarget.dataset.scene,
            index: e.currentTarget.dataset.index,
            id: e.currentTarget.dataset.id,
            sceneDescription: e.currentTarget.dataset.scenedesc,
        });
    },
    //删除网络场景
    deleteScene(e) {
        let _that = this;
        if (_that.data.showEdit) {
            if (e.currentTarget.dataset.num) {
                wx.navigateTo({
                    url: `./deleteChannel/deleteChannel?id=${e.currentTarget.dataset.id}&name=${e.currentTarget.dataset.name}&num=${e.currentTarget.dataset.num}&type=${_that.data.type}`,
                });
            } else {
                wx.showModal({
                    title: '提示',
                    content: '确认删除？',
                    success: function (res) {
                        if (res.confirm) {
                            wx.request({
                                url:
                                    app.globalData.rootPath +
                                    `cjMap/deleteNetFrequencyChannel?authId=${app.globalData.authId}&id=${e.currentTarget.dataset.id}`,
                                method: 'GET',
                                header: {
                                    'content-type': 'application/json',
                                },
                                success: function (res) {
                                    console.log(res);
                                    _that.setData({
                                        deviceContent: [],
                                        inputValue: '',
                                    });
                                    _that.getNetDeviceList();
                                },
                            });
                        } else {
                            console.log('点击取消回调');
                        }
                    },
                });
            }
        }
    },
    //弹窗
    //添加网络场景
    addDevice(e) {
        let _that = this;
        if (!_that.data.inputValue) {
            wx.showToast({
                title: '请填写分类名称',
                icon: 'none',
                duration: 2000,
            });
            return;
        }
        if (_that.data.showEdit) {
            //编辑
            let arr = 'deviceContent[' + _that.data.index + '].channelname';
            _that.setData({
                [arr]: _that.data.inputValue,
                showCover: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/editNetFrequencyChannel?authId=${app.globalData.authId}`,
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                },
                data: {
                    delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                    channelname: _that.data.inputValue,
                    channeldesc: _that.data.sceneDescription,
                    id: _that.data.id,
                    type: _that.data.type,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        wx.showToast({
                            title: 'OK',
                            icon: 'success',
                            duration: 2000,
                        });
                    } else {
                        wx.showToast({
                            title: '提交失败，请重新录入或联系后台管理',
                            icon: 'none',
                            duration: 2000,
                        });
                    }
                    _that.setData({
                        deviceContent: [],
                        inputValue: '',
                    });
                    _that.getNetDeviceList();
                },
            });
        } else {
            //添加
            _that.setData({
                showCover: false,
            });
            wx.request({
                url:
                    app.globalData.rootPath +
                    `cjMap/createNetFrequencyChannel?authId=${app.globalData.authId}`,
                method: 'POST',
                header: {
                    'content-type': 'application/json',
                },
                data: {
                    delflg: '00', //删除标志 00未删除 01已删除 02待审核10草稿
                    channelname: _that.data.inputValue,
                    channeldesc: _that.data.sceneDescription,
                    type: _that.data.type,
                },
                success: function (res) {
                    console.log(res);
                    if (res.data.status == 0) {
                        wx.showToast({
                            title: 'OK',
                            icon: 'success',
                            duration: 2000,
                        });
                    } else {
                        wx.showToast({
                            title: '提交失败，请重新录入或联系后台管理',
                            icon: 'none',
                            duration: 2000,
                        });
                    }
                    _that.setData({
                        deviceContent: [],
                        inputValue: '',
                    });
                    _that.getNetDeviceList();
                },
            });
        }
    },
    hideCover(e) {
        this.setData({
            showCover: false,
        });
    },
    stopEvent(e) {
        return;
    },

    // 获取频段号列表
    getNetDeviceList() {
        let _that = this;
        wx.request({
            url:
                app.globalData.rootPath +
                `cjMap/getNetFrequencyChannelList?authId=${app.globalData.authId}&type=${_that.data.type}&createtimeorder=asc`,
            method: 'GET',
            header: {
                'content-type': 'application/json',
            },
            success: function (res) {
                console.log(res);
                let btn = false;
                let idArr = _that.data.idArr.split(',');
                for (let i = 0; i < res.data.data.length; i++) {
                    if (idArr.includes(res.data.data[i].id)) {
                        res.data.data[i].select = true;
                        btn = true;
                    } else {
                        res.data.data[i].select = false;
                    }
                }
                _that.setData({
                    deviceContent: res.data.data,
                    btnActive: btn,
                });
            },
        });
    },
    // 确认返回上一页
    backPage(e) {
        if (!this.data.btnActive) return;
        let pages = getCurrentPages();
        let arr = this.data.deviceContent;
        let item = '';
        let itemId = '';
        for (let i = 0; i < arr.length; i++) {
            if (arr[i].select) {
                item = item
                    ? item + ',' + arr[i].channelname
                    : arr[i].channelname;
                itemId = itemId ? itemId + ',' + arr[i].id : arr[i].id;
            }
        }
        let prevPage = pages[pages.length - 2]; //上一页面
        //00--LTE频段  01--NR频段
        if (this.data.type == '00') {
            prevPage.setData({
                ltechannelsupport: item, //频段支持
                ltechannelsupportid: itemId,
            });
        }
        if (this.data.type == '01') {
            prevPage.setData({
                nrchannelsupport: item, //频段支持
                nrchannelsupportid: itemId,
            });
        }
        wx.navigateBack({
            delta: 1,
        });
    },
});
